package cn.edu.hit.education.view;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.edu.hit.education.action.AddNewCourseAction;
import cn.edu.hit.education.action.CodeCourseAction;
import cn.edu.hit.education.action.ExportAction;
import cn.edu.hit.education.action.ExportCourseInformationAction;
import cn.edu.hit.education.action.ImportAction;
import cn.edu.hit.education.cellmodifier.CourseInformationCellModifier;
import cn.edu.hit.education.contentprovider.CourseInformationContentProvider;
import cn.edu.hit.education.contentprovider.StatisticsInfoContentProvider;
import cn.edu.hit.education.labelprovider.CourseInformationLabelProvider;
import cn.edu.hit.education.labelprovider.StatisticsInfoLabelProvider;
import cn.edu.hit.education.listener.DoubleDigitVerifyListener;
import cn.edu.hit.education.model.StatisticsInfo;
import cn.edu.hit.education.pojo.College;
import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.ExCourse;
import cn.edu.hit.education.pojo.Process;
import cn.edu.hit.education.pojo.Property;
import cn.edu.hit.education.pojo.Schedule;
import cn.edu.hit.education.pojo.Semester;
import cn.edu.hit.education.pojo.Specialty;
import cn.edu.hit.education.pojo.University;
import cn.edu.hit.education.pojo.User;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.service.IProcessService;
import cn.edu.hit.education.service.IPropertyService;
import cn.edu.hit.education.service.IScheduleService;
import cn.edu.hit.education.service.ISemesterService;
import cn.edu.hit.education.service.ISpecialtyService;
import cn.edu.hit.education.service.IUserSpeciatyService;
import cn.edu.hit.education.utils.StatisticsUtil;
import educationschedule.Application;

@Component
public class CourseInformationView extends ViewPart {
	public static final String ID = "cn.edu.hit.education.view.CourseInformationView";
	String[] columnProperties = new String[] { "number", "name", "credicts",
			"period", "theoretical", "experiment"};

	private Table tableCourse;
	TableViewer tableViewerCourse;
	private Table tableStatisticsInfo;
	TableViewer tableViewerStatisticsInfo;
	
	private Combo comboProcess;
	private Combo comboProperty;

	@Autowired
	IProcessService processService;
	@Autowired
	IPropertyService propertyService;
	@Autowired
	IScheduleService scheduleService;
	@Autowired
	ICourseService courseService;
	@Autowired
	ISemesterService semesterService;
	@Autowired
	ISpecialtyService specialtyService;
	@Autowired
	IUserSpeciatyService userSpeciatyService;

	private Semester semester;
	List<Course> courses = new ArrayList<Course>();
	List<ExCourse> exCourses = new ArrayList<ExCourse>();
	List<StatisticsInfo> statisticsInfos = new ArrayList<StatisticsInfo>();
	
	private Combo comboUniversity;
	private Combo comboCollege;
	private Combo comboSpecialty;
	
	private University university;
	private College college;
	private Specialty specialty;
	
	private List<College> colleges = new ArrayList<College>();
	private List<Specialty> specialties = new ArrayList<Specialty>();

	private IAction addCourseAction = null;
	private IAction codeCourseAction = null;
	private IAction importAction = null;
	private IAction exportAction = null;
	private IAction exportCourseInformationAction = null;

	public CourseInformationView() {
	}

	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FormLayout());
		
		Composite composite_top = new Composite(composite, SWT.NONE);
		composite_top.setLayoutData(new FormData());
		composite_top.setLayout(new GridLayout(10, false));
		FormData fd_composite_top = new FormData();
		fd_composite_top.top = new FormAttachment(0, 0);
		fd_composite_top.right = new FormAttachment(100, 0);
		fd_composite_top.left = new FormAttachment(0,0);
		fd_composite_top.bottom = new FormAttachment(70,0);
		composite_top.setLayoutData(fd_composite_top);

		Label label_3 = new Label(composite_top, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,	1, 1));
		label_3.setText("学校");

		comboUniversity = new Combo(composite_top, SWT.NONE);
		comboUniversity.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,	false, 1, 1));

		Label label_2 = new Label(composite_top, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("学院");

		comboCollege = new Combo(composite_top, SWT.NONE);
		comboCollege.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				college = colleges.get(comboCollege.getSelectionIndex());
				if (specialties == null || specialties.size() != 1) {
					comboSpecialty.removeAll();
					specialties = specialtyService.querySpecialtiesByCollegeId(college.getId());
					initSpecialtyCombo(specialties);
				}
			}
		});
		comboCollege.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,	false, 1, 1));

		Label label_1 = new Label(composite_top, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,	1, 1));
		label_1.setText("专业");

		comboSpecialty = new Combo(composite_top, SWT.NONE);
		comboSpecialty.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				specialty = specialties.get(comboSpecialty.getSelectionIndex());
				clearAllCourses();
			}
		});
		comboSpecialty.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblNewLabel = new Label(composite_top, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("\u6559\u80B2\u8FC7\u7A0B");

		comboProcess = new Combo(composite_top, SWT.NONE);
		comboProcess.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List<Process> processes_ = processService.queryProcessesByProcessName(comboProcess.getText());
				if (processes_ != null && processes_.size() > 0) {
					initPropertyCombo(processes_.get(0).getId());
				}
			}
		});
		comboProcess.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,	false, 1, 1));

		Label label = new Label(composite_top, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("\u8BFE\u7A0B\u6027\u8D28");

		comboProperty = new Combo(composite_top, SWT.NONE);
		comboProperty.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refreshData();
			}
		});
		comboProperty.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		tableViewerCourse = new TableViewer(composite_top, SWT.BORDER	| SWT.FULL_SELECTION);
		tableCourse = tableViewerCourse.getTable();
		tableCourse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int selectedIndex = tableCourse.getSelectionIndex();
				if (selectedIndex < courses.size()) {
				}
			}
		});
		tableCourse.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 10, 1));
		tableCourse.setSize(297, 469);
		tableCourse.setLinesVisible(true);
		tableCourse.setHeaderVisible(true);

		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tableColumn = tableViewerColumn.getColumn();
		tableColumn.setWidth(100);
		tableColumn.setText("\u8BFE\u7A0B\u4EE3\u7801");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tableColumn_1 = tableViewerColumn_1.getColumn();
		tableColumn_1.setWidth(200);
		tableColumn_1.setText("\u8BFE\u7A0B\u540D\u79F0");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tableColumn_2 = tableViewerColumn_2.getColumn();
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("\u5B66\u5206");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tableColumn_3 = tableViewerColumn_3.getColumn();
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("\u5B66\u65F6");

		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tableColumn_4 = tableViewerColumn_4.getColumn();
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("\u7406\u8BBA\u5B66\u65F6");

		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn_5.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("\u5B9E\u9A8C\u5B66\u65F6");

		Menu contextMenu = new Menu(tableCourse);
		tableCourse.setMenu(contextMenu);

		MenuItem modifyItem = new MenuItem(contextMenu, SWT.None);
		modifyItem.setEnabled(false);
		modifyItem.setText("去除");

		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewerCourse, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("\u5B66\u671F");

		TableColumn tblclmnNewColumn_2 = new TableColumn(tableCourse, SWT.NONE);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("\u662F\u5426\u5F00\u8BFE");
		modifyItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] selections = tableCourse.getSelection();
				Shell parentShell = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				if (selections.length > 0) {
					for (TableItem tableItem : selections) {
						Course theCourse = (Course) tableItem.getData();

						// 获取当前可查看专业的Id
						List<Integer> specityIds = specialties.stream()
								.map(item -> {
									return item.getId();
								}).collect(Collectors.toList());

						// List<Schedule> schedules =
						// scheduleService.querySchedulesBySpecialtySemesterCourseId(specialty.getId(),
						// semester.getId(),theCourse.getId());
						List<Schedule> schedules1 = scheduleService
								.querySchedulesBySemesterCourseId(
										semester.getId(), theCourse.getId());
						// 过滤掉所有跟用户无关的专业
						List<Schedule> schedules = schedules1.stream()
								.filter(item -> {
									return specityIds.contains(item.getId());
								}).collect(Collectors.toList());

						if (schedules != null && schedules.size() > 0) {
							for (Schedule schedule : schedules) {
								scheduleService.deleteByPrimaryKey(schedule
										.getId());
							}
						}
					}

				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		tableCourse.addListener(SWT.MouseDown, new Listener() {

			@Override
			public void handleEvent(Event event) {
				TableItem[] selections = tableCourse.getSelection();
				if (selections.length > 0) {
					modifyItem.setEnabled(true);
				} else {
					modifyItem.setEnabled(false);
				}
			}
		});

		tableViewerCourse.setColumnProperties(columnProperties);
		CellEditor[] cellEditors = new CellEditor[8];
		cellEditors[0] = new TextCellEditor(tableCourse);
		cellEditors[1] = new TextCellEditor(tableCourse);
		cellEditors[2] = new TextCellEditor(tableCourse);
		cellEditors[3] = new TextCellEditor(tableCourse);
		cellEditors[4] = new TextCellEditor(tableCourse);
		cellEditors[5] = new TextCellEditor(tableCourse);
		//cellEditors[6] = new ComboBoxCellEditor(tableCourse,CourseInformationCellModifier.SEMESTERS, SWT.READ_ONLY);
		//cellEditors[7] = new ComboBoxCellEditor(tableCourse,CourseInformationCellModifier.OPENS, SWT.READ_ONLY);
		tableViewerCourse.setCellEditors(cellEditors);

		ICellModifier modifier = new CourseInformationCellModifier(this,tableViewerCourse);
		tableViewerCourse.setCellModifier(modifier);

		tableViewerCourse.setContentProvider(new CourseInformationContentProvider());
		tableViewerCourse.setLabelProvider(new CourseInformationLabelProvider());

		// 验证数字
		for (int i = 2; i < 6; i++) {
			Text text = (Text) cellEditors[i].getControl();
			text.addVerifyListener(new DoubleDigitVerifyListener());
		}

		
		///////////////////////////////////////////////////////////////
		Composite composite_bottom = new Composite(composite, SWT.NONE);
		FormData fd_composite_bottom = new FormData();
		fd_composite_bottom.top = new FormAttachment(70, 0);
		fd_composite_bottom.right = new FormAttachment(100, -5);
		fd_composite_bottom.bottom = new FormAttachment(100, 0);
		fd_composite_bottom.left = new FormAttachment(0, 5);
		composite_bottom.setLayoutData(fd_composite_bottom);
		composite_bottom.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		tableViewerStatisticsInfo = new TableViewer(composite_bottom, SWT.BORDER | SWT.FULL_SELECTION);
		tableStatisticsInfo = tableViewerStatisticsInfo.getTable();
		tableStatisticsInfo.setHeaderVisible(true);
		tableStatisticsInfo.setLinesVisible(true);
		
		TableViewerColumn tableViewerColumn_21 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tableColumn_21 = tableViewerColumn_21.getColumn();
		tableColumn_21.setWidth(100);
		tableColumn_21.setText("统计信息名称");
		
		TableViewerColumn tableViewerColumn_22 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tblclmnNewColumn_22 = tableViewerColumn_22.getColumn();
		tblclmnNewColumn_22.setWidth(100);
		tblclmnNewColumn_22.setText("学分");
		
		TableViewerColumn tableViewerColumn_23 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tblclmnNewColumn_23 = tableViewerColumn_23.getColumn();
		tblclmnNewColumn_23.setWidth(100);
		tblclmnNewColumn_23.setText("学时");
		
		TableViewerColumn tableViewerColumn_24 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tblclmnNewColumn_24 = tableViewerColumn_24.getColumn();
		tblclmnNewColumn_24.setWidth(100);
		tblclmnNewColumn_24.setText("理论学时");
		
		TableViewerColumn tableViewerColumn_25 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tblclmnNewColumn_25 = tableViewerColumn_25.getColumn();
		tblclmnNewColumn_25.setWidth(100);
		tblclmnNewColumn_25.setText("实践学时");
		
		TableViewerColumn tableViewerColumn_26 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tableColumn_26 = tableViewerColumn_26.getColumn();
		tableColumn_26.setWidth(100);
		tableColumn_26.setText("单位");
		
		TableViewerColumn tableViewerColumn_27 = new TableViewerColumn(tableViewerStatisticsInfo, SWT.NONE);
		TableColumn tableColumn_27 = tableViewerColumn_27.getColumn();
		tableColumn_27.setWidth(100);
		tableColumn_27.setText("方式");

		tableViewerStatisticsInfo.setContentProvider(new StatisticsInfoContentProvider());
		tableViewerStatisticsInfo.setLabelProvider(new StatisticsInfoLabelProvider());
		tableViewerStatisticsInfo.setInput(statisticsInfos);
		
		initDepartments();
		initProcessCombo();
		// ///////////////

		makeActions();
		initializeToolBar();
	}

	@Override
	public void setFocus() {

	}

	private void makeActions() {
		addCourseAction = new AddNewCourseAction(tableViewerCourse);
		codeCourseAction = new CodeCourseAction();
		importAction = new ImportAction();
		exportAction = new ExportAction();
		exportCourseInformationAction = new ExportCourseInformationAction(exCourses);
	}

	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
		toolbarManager.add(addCourseAction);
		//toolbarManager.add(codeCourseAction);
		toolbarManager.add(importAction);
		toolbarManager.add(exportAction);
		toolbarManager.add(exportCourseInformationAction);
	}

	private List<Course> queryCourseByProperty(Specialty specialty,Property property) {
		return courseService.queryCoursesBySpecialtyPropertyId(specialty.getId(), property.getId());
	}

	public void addNewCourse(int id) {
		Course course = courseService.queryCourseByPrimaryKey(id);

		if (!isSameCourse(course.getId(), 0, semester.getId())) {
			Schedule schedule = new Schedule();
			schedule.setCourseId(course.getId());
			schedule.setSpecialtyId(null);
			schedule.setSemesterId(semester.getId());
			scheduleService.insert(schedule);
			// refreshData();
		} else {
			MessageDialog.openConfirm(PlatformUI.getWorkbench().getDisplay()
					.getActiveShell(), "提示", "进程表中已包含该课程！");
		}
	}

	private boolean isSameCourse(int courseId, int specialtyId, int semesterId) {
		List<Schedule> theSchedules = scheduleService
				.querySchedulesBySemesterId(semesterId);
		if (theSchedules != null && theSchedules.size() > 0) {
			for (Schedule schedule : theSchedules) {
				if (schedule.getCourseId() == courseId) {
					return true;
				}
			}
		}
		return false;
	}

	private void initSpecialties() {
		Subject subject = SecurityUtils.getSubject();
		// 角色是校长，可查看表中所有专业对应的课程信息
		if (subject.hasRole("principal")) {
			specialties = specialtyService.queryAllSpecialty();
			// 角色是院长，专业初始化为本学院下的所有专业！
		} else if (subject.hasRole("president")) {
			User user = (User) subject.getPrincipal();
			// 根据院长的用户Id查询到关联的专业Id
			List<Integer> ids = userSpeciatyService.querySpeciatyIdsByUser(user
					.getId());
			specialties = specialtyService.queryRelativeSpecialty(ids);
		} else if (subject.hasRole("director")) {
			User user = (User) subject.getPrincipal();
			// 根据普通用户的用户Id查询到关联的专业Id
			List<Integer> ids = userSpeciatyService.querySpeciatyIdsByUser(user
					.getId());
			specialties = specialtyService.queryRelativeSpecialty(ids);
		}

	}

	private void initProcessCombo() {
		List<Process> processes = processService.queryAllProcess();
		if (processes != null && processes.size() > 0) {
			for (Process process : processes) {
				comboProcess.add(process.getName());
			}

			comboProcess.select(0);
			List<Process> processes_ = processService
					.queryProcessesByProcessName(comboProcess.getText());
			if (processes_ != null && processes_.size() > 0) {
				initPropertyCombo(processes_.get(0).getId());
			}
		}
	}

	private void initPropertyCombo(int processId) {
		comboProperty.removeAll();
		List<Property> properties = propertyService
				.queryPropertiesByProcessId(processId);
		if (properties != null && properties.size() > 0) {
			for (Property property : properties) {
				comboProperty.add(property.getName());
			}

			comboProperty.select(0);
			refreshData();
		}
	}

	private void refreshData() {
		Property theProperty = null;
		String processName = comboProcess.getText();
		String propertyName = comboProperty.getText().trim();
		List<Process> processes_ = processService.queryProcessesByProcessName(processName);
		if (processes_ != null && processes_.size() > 0) {
			int processId = processes_.get(0).getId();

			List<Property> properties = propertyService.queryPropertiesByProcessId(processId);
			if (properties != null && properties.size() > 0) {
				for (Property property : properties) {
					if (propertyName.equals(property.getName().trim())) {
						theProperty = property;
					}
				}
			}

			if (theProperty != null) {
				courses = queryCourseByProperty(theProperty);
				exCourses.clear();
				completeCourseInformation(courses, exCourses);
				statisticsInfos = StatisticsUtil.processCourseInformationData(exCourses);
				tableViewerCourse.setInput(exCourses);
				tableViewerStatisticsInfo.setInput(statisticsInfos);
			}
		}
	}

	private List<Course> queryCourseByProperty(Property property) {
		// 获取当前可查看专业的Id
		List<Integer> specityIds = specialties.stream().map(item -> {
			return item.getId();
		}).collect(Collectors.toList());
		// 根据property找到所有课程
		List<Course> coursesByPropertyId = courseService
				.queryCoursesByPropertyId(property.getId());
		// 过滤掉不熟属于本用户权限的专业
		List<Course> collect = coursesByPropertyId.stream().filter(item -> {
			return specityIds.contains(item.getSpecialtyId());
		}).collect(Collectors.toList());

		return collect;
	}

	private void completeCourseInformation(List<Course> courses, List<ExCourse> exCourses) {
		for (Course course : courses) {
			ExCourse exCourse = new ExCourse();
			exCourse.setId(course.getId());
			exCourse.setSpecialtyId(course.getSpecialtyId());
			exCourse.setPropertyId(course.getPropertyId());
			exCourse.setName(course.getName());
			exCourse.setCredits(course.getCredits());
			exCourse.setImportant(course.getImportant());
			exCourse.setNumber(course.getNumber());
			exCourse.setPeriod(course.getPeriod());
			exCourse.setTheoretical(course.getTheoretical());
			exCourse.setExperiment(course.getExperiment());
			exCourse.setUnit(course.getUnit());
			exCourse.setMethod(course.getMethod());
			exCourse.setStart(course.getStart());
			exCourse.setEnd(course.getEnd());
			exCourse.setClassroom(course.getClassroom());
			exCourse.setSelected(course.getSelected());
			exCourse.setNote(course.getNote());

			
			// List<Schedule> schedules =
			// scheduleService.querySchedulesBySpecialtyCourseId(specialty.getId(),
			// course.getId());
			// 根据课程id查询到所有的计划
			List<Schedule> allSchedules = scheduleService.querySchedulesBySCourseId(course.getId());
			// //获取当前可查看专业的Id
			List<Integer> specityIds = specialties.stream().map(item -> {
				return item.getId();
			}).collect(Collectors.toList());

			// 过滤掉不属于本用户权限的课程
			List<Schedule> schedules = allSchedules.stream().filter(item -> {
				return specityIds.contains(item.getSpecialtyId());
			}).collect(Collectors.toList());
/*
			if (schedules != null && schedules.size() > 0) {
				Schedule theSchedule = schedules.get(0);
				Semester theSemester = semesterService.queryByPrimaryKey(theSchedule.getSemesterId());
				exCourse.setSemesterName(theSemester.getNote());
				exCourse.setOpened(theSchedule.getOpened() == null ? 0 : theSchedule.getOpened());
			}*/
			
			exCourse.setSchedules(schedules);
			exCourses.add(exCourse);
		}
	}

	private void initDepartments(){
		initUniversityCombo();
		initCollegeCombo();
		initSpecialtyCombo(null);
	}
	
	private void initUniversityCombo(){
		university = Application.authenticationInformation.getCurrentUniversity();
		if(university != null){// 有校长权限
			comboUniversity.add(university.getName());
			comboUniversity.select(0);
		}
	}
	
	private void initCollegeCombo(){
		colleges = Application.authenticationInformation.getCurrentColleges();
		if(colleges != null && colleges.size() > 0){//院长
			comboCollege.removeAll();
			for (College college : colleges) {
				comboCollege.add(college.getName());
			}
			
			if(colleges.size() == 1){
				comboCollege.select(0);
				college = colleges.get(0);
			}
		}	
	}
	
	private void initSpecialtyCombo(List<Specialty> specialties2){
		specialties = Application.authenticationInformation.getCurrentSpecialties();
		if(specialties2 != null){
			specialties = specialties2;
		}
		
		if(specialties != null && specialties.size() > 0){//系主任
			comboSpecialty.removeAll();
			for (Specialty specialty : specialties) {
				comboSpecialty.add(specialty.getName());
			}
			
			if(specialties.size() == 1){
				comboSpecialty.select(0);
				specialty = specialties.get(0);
			}
		}
	}
	
	private void clearAllCourses(){
		exCourses.clear();
		tableViewerCourse.setInput(exCourses);
		
		statisticsInfos.clear();
		tableViewerStatisticsInfo.setInput(statisticsInfos);
	}
	
	public void updateDatabase(ExCourse course){
		int count = courseService.update(course);
		if(count > 0){
			
		}
	}
}